package com.ruoyi.project.mall.service.impl;

import cc.openkit.common.KitUtil;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.utils.bootdo.MD5Utils;
import com.ruoyi.common.utils.bootdo.PageHelperUtils;
import com.ruoyi.common.utils.bootdo.R;
import com.ruoyi.common.utils.bootdo.UuidUtil;
import com.ruoyi.project.mall.dao.MallShopUserDao;
import com.ruoyi.project.mall.domain.MallShopUserDO;
import com.ruoyi.project.mall.service.MallShopUserService;
import com.ruoyi.project.men.dao.MenNeedInfoDao;
import com.ruoyi.project.wx.dao.GoodsGoodsDao;
import com.ruoyi.project.wx.dao.MemberCodeDao;
import com.ruoyi.project.wx.dao.MemberCollectDao;
import com.ruoyi.project.wx.dao.MemberUserDao;
import com.ruoyi.project.wx.domain.MemberCodeDO;
import com.ruoyi.project.wx.domain.MemberUserDO;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Service
public class MallShopUserServiceImpl implements MallShopUserService {
	@Autowired
	private MallShopUserDao mallShopUserDao;
	@Autowired
	private MemberCodeDao memberCodeDao;
	@Autowired
	private MemberUserDao memberUserDao;
	@Autowired
	private MemberCollectDao memberCollectDao;
	@Autowired
	private GoodsGoodsDao goodsGoodsDao;
	@Autowired
	private MenNeedInfoDao menNeedInfoDao;
	
	@Override
	public MallShopUserDO get(String id){
		return mallShopUserDao.get(id);
	}
	
	@Override
	public List<MallShopUserDO> list(Map<String, Object> map){
		return mallShopUserDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return mallShopUserDao.count(map);
	}
	
	@Override
	public int save(MallShopUserDO mallShopUser){
		return mallShopUserDao.save(mallShopUser);
	}
	
	@Override
	public int update(MallShopUserDO mallShopUser){
		return mallShopUserDao.update(mallShopUser);
	}
	
	@Override
	public int remove(String id){
		return mallShopUserDao.remove(id);
	}
	
	@Override
	public int batchRemove(String[] ids){
		return mallShopUserDao.batchRemove(ids);
	}

    @Override
    public List<MallShopUserDO> likeList(Map<String, Object> map){
        return mallShopUserDao.likeList(map);
    }
    @Override
    public int likeListCount(Map<String, Object> map){
        return mallShopUserDao.likeListCount(map);
    }

    @Override
	@Transactional(isolation = Isolation.REPEATABLE_READ, propagation = Propagation.REQUIRED)
	public R shopUserRegister(Map<String, Object> params) {

		try {
			//查看验证码
			params.put("delFlag",0);
			List<MemberCodeDO> list = memberCodeDao.list(params);
			if(list.size()<=0){
				return R.error("验证码有误");
			}
			//查看手机号
			MemberUserDO memberUserDO = memberUserDao.findByPhone(params.get("phone").toString());
			if(memberUserDO!=null){
				return R.error("手机号已注册");
			}else{
				memberUserDO = new MemberUserDO();
				memberUserDO.setMoney(new BigDecimal("0.00"));
				memberUserDO.setId(UuidUtil.get32UUID());
				memberUserDO.setFreeze(new BigDecimal("0.00"));
				memberUserDO.setDisabled(0);
				memberUserDO.setDelFlag(0);
				memberUserDO.setSex(0);
				memberUserDO.setCreateAt(new Date());
				memberUserDO.setName("昵称"+ KitUtil.getStringRandom(8));

				memberUserDO.setAvator("https://tse2-mm.cn.bing.net/th/id/OIP.IykEwu6UUNOvq9LFU0d3kwAAAA?w=164&h=180&c=7&o=5&dpr=1.25&pid=1.7");
				memberUserDO.setLevel(1);
				String encrypt = MD5Utils.encrypt(params.get("phone").toString(), "123456");
				memberUserDO.setPassword(encrypt);
				memberUserDO.setPhone(params.get("phone").toString());
				memberUserDO.setUpdateAt(new Date());

				MallShopUserDO mallShopUserDO = new MallShopUserDO();
				BeanUtils.populate(mallShopUserDO,params);
				mallShopUserDO.setCaAt(new Date());
				mallShopUserDO.setDelFlag(0);
				mallShopUserDO.setId(UuidUtil.get32UUID());
				mallShopUserDO.setStatus(0);
				mallShopUserDO.setTag(0);
				mallShopUserDO.setUserId(memberUserDO.getId());
				mallShopUserDO.setUpAt(new Date());
				String encrypt1 = MD5Utils.encrypt(params.get("phone").toString(), "123456");
				mallShopUserDO.setPassword(encrypt1);
				memberUserDao.save(memberUserDO);
				mallShopUserDao.save(mallShopUserDO);


				return R.ok();
			}
		} catch (Exception e) {
			e.printStackTrace();
			return R.error();
		}

    }

    @Override
    public R getShopInfo(Map<String, Object> params) {
		//店铺信息
		Map<String,Object> shopMap = mallShopUserDao.getShopInfo(params.get("shopId").toString());
		//是否收藏
		params.put("collectId",params.get("shopId").toString());
		params.put("status","1");
		params.put("delFlag","0");
		int count = memberCollectDao.count(params);
		shopMap.put("isCollect",count);
		return R.ok().put("data",shopMap);


		//店铺需求*/


	}

	@Override
	public R getShopGoods(Map<String, Object> params) {
		PageHelperUtils.page(params);
		//店铺商品
		List<Map<String,Object>> goodsList = goodsGoodsDao.getByShopId(params.get("shopId").toString());
		PageInfo<Map<String,Object>> pageInfo = new PageInfo<>(goodsList);
		return R.ok().put("data",pageInfo);
	}

	@Override
	public R getShopNeed(Map<String, Object> params) {
		MallShopUserDO mallShopUserDO = mallShopUserDao.get(params.get("shopId").toString());
		PageHelperUtils.page(params);
		params.put("shopUserId",mallShopUserDO.getUserId());
		List<Map<String,Object>> needList =  menNeedInfoDao.getShopNeed(params);
		PageInfo<Map<String,Object>> pageInfo = new PageInfo<>(needList);
		return R.ok().put("data",pageInfo);
	}

}
